<template>
    <div>
        <div>
            <el-button size="small" icon="el-icon-circle-plus" type="primary" @click="addRp">新增奖罚类别</el-button>
        </div>
        <div style="margin-top: 10px">
            <el-table
                    :data="rps"
                    border
                    stripe
                    @selection-change="handleSelectionChange"
                    size="small"
                    style="width: 80%">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="奖罚名称"
                        width="150">
                </el-table-column>
                <el-table-column
                        prop="description"
                        label="奖罚说明"
                        :show-overflow-tooltip="true"
                        width="300">
                </el-table-column>
                <el-table-column
                        label="是否启用"
                        width="80">
                    <template slot-scope="scope">
                        <el-tag type="success" v-if="scope.row.enabled">已启用</el-tag>
                        <el-tag type="info" v-else>未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        label="涉及金额"
                        width="150">
                    <template slot-scope="scope">
                        <i class="el-icon-money"></i>
                        <span style="margin-left: 10px">{{ scope.row.money}}元</span>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="createTime"
                        label="创建时间"
                        width="150">
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="showEdit(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-dialog
                    @close="viewClose"
                    title="新增奖罚类别"
                    :visible.sync="dialogVisible"
                    width="30%">
                <div>
                    <el-tag>奖罚名称:</el-tag>
                    <el-input v-model="rp.name" placeholder="请输入奖罚名称" size="small" class="dia_input"></el-input>
                    <div style="margin: 20px 0;"></div>
                    <el-tag>奖罚说明:</el-tag>
                    <div style="margin: 20px 0;"></div>
                    <el-input
                            type="textarea"
                            :autosize="{ minRows: 2, maxRows: 4}"
                            placeholder="请输入奖罚说明"
                            size="small"
                            v-model="rp.description">
                    </el-input>
                    <div style="margin: 20px 0;"></div>
                    <div>
                        <el-tag>是否启用:</el-tag>
                        <el-switch
                                class="dia_input"
                                v-model="rp.enabled"
                                active-text="启用"
                                inactive-text="禁用"
                                active-color="#13ce66"
                                inactive-color="#ff4949">
                        </el-switch>
                    </div>
                    <div style="margin: 20px 0;"></div>
                    <el-tag>涉及金额:</el-tag>
                    <el-input v-model="rp.money" placeholder="请输入涉及金额" size="small" class="dia_input"></el-input>
                </div>
                <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false" size="small">取 消</el-button>
                <el-button type="primary" @click="addOrUpdateRp" size="small">确 定</el-button>
            </span>
            </el-dialog>
<!--            <el-button type="danger" @click="batchDelete" size="small" style="margin-top: 8px"-->
<!--                       :disabled="multipleSelection.length==0">批量删除-->
<!--            </el-button>-->
        </div>
    </div>
</template>

<script>
    export default {
        name: "EcManager",
        data(){
            return{
                rps:[],
                multipleSelection:[],
                rp:{
                    name:'',
                    description:'',
                    money:null,
                    enabled:false
                },
                dialogVisible:false
            }
        },
        mounted() {
            this.getRps();
        },
        methods:{
            viewClose(){
                this.rp={
                    name:'',
                    description:'',
                    money:null,
                    enabled:false
                };
            },
            addRp(){
              this.dialogVisible = true;
            },
            addOrUpdateRp(){
                if(this.rp.id){
                    this.putRequest("/system/basic/rp/",this.rp).then(resp=>{
                        if(resp){
                            this.getRps();
                        }
                    })
                    this.dialogVisible = false;
                }else{
                    // console.log(this.rp);
                    // this.rp.money = parseFloat(this.rp.money);
                    this.postRequest("/system/basic/rp/",this.rp).then(resp=>{
                        if(resp){
                            this.getRps();
                        }
                    })
                    this.dialogVisible = false;
                }
            },
            getRps(){
                this.getRequest("/system/basic/rp/").then(resp=>{
                    if(resp){
                        this.rps = resp;
                    }
                })
            },
            handleSelectionChange(){

            },
            showEdit(index, data){
                this.rp =JSON.parse(JSON.stringify(data));
                this.dialogVisible = true;
            },
            handleDelete(index,data){
                this.$confirm('此操作将永久删除【' + data.name + '】奖罚分类, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/rp/" + data.id).then(resp => {
                        if (resp) {
                            this.getRps();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }
    }
</script>

<style>
    .dia_input {
        width: 200px;
        margin-left: 10px
    }
    body .el-table th.gutter {
        display: table-cell !important
    }
</style>